package bdu;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import bdu.test.RobotTestBed;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * Illustrates JUnit testing of Robocode robots. This test verifies that scavenger 
 * conserves energy by not firing on moving targets.
 * 
 * @author Bao Huy Ung
 * 
 */
public class TestScavengerFireNonStationary extends RobotTestBed {

  double lastXCoord;
  double lastYCoord;
  boolean fireCheck;

  /**
   * Specifies that SpinBot and Scavenger are to be matched up in this test.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SpinBot,bdu.Scavenger";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Checks to see if there is any fire while a target is not stationary by looking at gun heat.
   * 
   * @param event End of the robots turn cycle
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // Get and store information on robots
    IRobotSnapshot enemy = event.getTurnSnapshot().getRobots()[0];
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double yCoord = enemy.getY();
    double xCoord = enemy.getX();
   
    // If enemy's coordinates changed from what it was last, it has moved.
    if (lastXCoord != xCoord || lastYCoord != yCoord) {
      //if there is gun heat then it fails the fire test
      if (robot.getGunHeat() > 0) {
        fireCheck = false;
      }
      else {
        fireCheck = true;
      }
    }
    
    // Update the enemie's new known coordinates.
    lastXCoord = xCoord;
    lastYCoord = yCoord;

  }

  /**
   * The actual test, which asserts that scavenger did not fire on the constantly moving SpinBot.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Fired Upon Stationary Target", fireCheck);
  }
}